package com.csu.soa_library.common;

import com.csu.soa_library.entity.Account;
import com.csu.soa_library.entity.Book;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.Getter;
import org.springframework.data.domain.Page;

import java.util.List;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse <T> {

    private int code;
    private String message;
    //书和连接
    private DataWrapper<T> data;
    //书S和连接
    private DataWrapper2<T> data2;

    private DataWrapper3<T> data4;
    //连接
    private List<T> data3;

    private CommonResponse(int code, String message, DataWrapper<T> data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    private CommonResponse(int code, String message, DataWrapper2<T> data) {
        this.code = code;
        this.message = message;
        this.data2 = data;
    }
    private CommonResponse(int code, String message, DataWrapper3<T> data) {
        this.code = code;
        this.message = message;
        this.data4 = data;
    }

    private CommonResponse(int code, String message, List<T> data) {
        this.code = code;
        this.message = message;
        this.data3 = data;
    }

    public CommonResponse(int code) {
        this.code = code;
    }

    public CommonResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    // 内部类，用于封装数据和链接
    public static class DataWrapper<T> {
        private Page<T> books;
        private List<Link> links;

        public DataWrapper(Page<T> books, List<Link> links) {
            this.books = books;
            this.links = links;
        }

        // Getters
        public Page<T> getBooks() {
            return books;
        }

        public List<Link> getLinks() {
            return links;
        }
    }

    public static class DataWrapper2<T> {
        private Book book;
        private List<Link> links;

        public DataWrapper2(Book books, List<Link> links) {
            this.book = books;
            this.links = links;
        }
        // Getters
        public Book getBooks() {
            return book;
        }

        public List<Link> getLinks() {
            return links;
        }
    }
    public static class  DataWrapper3<T>{
        private Account account;
        private List<Link> links;
        public DataWrapper3(Account account,List<Link> links){
            this.account= account;
            this.links = links;
        }

        public Account getAccount() {
            return account;
        }
        public List<Link> getLinks(){
            return links;
        }
    }

    // 内部类，用于封装单个链接的信息
    public static class Link {
        private String rel;
        private String href;
        private String method;

        public Link(String rel, String href, String method) {
            this.rel = rel;
            this.href = href;
            this.method = method;
        }

        // Getters
        public String getRel() {
            return rel;
        }

        public String getHref() {
            return href;
        }

        public String getMethod() {
            return method;
        }
    }


    public static <T> CommonResponse<T> createForSuccess(int code, String message, DataWrapper<T> data) {
        return new CommonResponse<>(code, message, data);
    }

    public static <T> CommonResponse<T> createForSuccess(int code, String message, DataWrapper2<T> data) {
        return new CommonResponse<>(code, message, data);
    }

    public static <T> CommonResponse<T> createForSuccess(int code, String message, DataWrapper3<T> data) {
        return new CommonResponse<>(code, message, data);
    }

    public static <T> CommonResponse<T> createForSuccess(int code, String message, List<T> data) {
        return new CommonResponse<>(code, message, data);
    }


    @JsonIgnore
    public boolean isSuccess() {
        return this.code == ResponseCode.SUCCESS.getCode();
    }

    public static <T> CommonResponse<T> createForSuccess() {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> CommonResponse<T> createForSuccessMessage(String message) {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), message);
    }

//    public static <T> CommonResponse<T> createForSuccess(T data) {
//        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), data);
//    }
//
//    public static <T> CommonResponse<T> createForSuccess(String message, T data) {
//        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(), message, data);
//    }



    public static <T> CommonResponse<T> createForError() {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDescription());
    }

    public static <T> CommonResponse<T> createForError(String message,List<T>data) {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(), message,data);
    }
    public static <T>CommonResponse<T> createForError(String message) {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(), message);
    }

    public static <T> CommonResponse<T> createForError(int code, String message) {
        return new CommonResponse<T>(code, message);
    }


}
